package com.ly.cloud.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ly.cloud.dto.AprvCourseDto;
import com.ly.cloud.entity.ApplyCoursePo;
import com.ly.cloud.entity.ApprovalDetailPo;
import com.ly.cloud.entity.CourseBasePo;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.ApplyCourseMapper;
import com.ly.cloud.mapper.ApprovalDetailMapper;
import com.ly.cloud.service.SpApplyCourseService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.ApprovalDetailVo;

/**
 * 查询课程申请审批信息实现类
 * 
 * @author siqi
 * @date 2018-12-22
 */
@Service
public class SpApplyCourseServiceImpl implements SpApplyCourseService {

	private static final String PASS = "1";// 通过
	private static final String NOPASS = "0";// 不通过
	private static final String SPZT_NOPASS = "8";// 审批不通过
	private static final String SPZT_PASS = "9";// 审批通过

	@Autowired
	private ApprovalDetailMapper approvalDetailMapper;
	@Autowired
	private ApplyCourseMapper applyCourseMapper;

	/**
	 * 查询课程申请审批信息（数据量较少未分页）
	 */
	@Override
	public List<ApprovalDetailVo> queryAprvDetail(String kcbh) {
		try {
			EntityWrapper<ApprovalDetailPo> ew = new EntityWrapper<>();
			ew.where("czbbh={0} and bdzjid={1} and sflsjl={2} and sfspcl={3}", "LY_CKBZ_GXJW_KCSJSQL", kcbh, "0", "1")
					.orderBy("spsx");
			List<ApprovalDetailPo> aprvDetailList = approvalDetailMapper.selectList(ew);
			return BeanCopyUtils.copyListProperties(aprvDetailList, ApprovalDetailVo.class);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("查询失败", e);
		}
	}

	/**
	 * 根据课程编号审批课程申请
	 */
	@Override
	@Transactional
	public boolean aprvCourse(String loginUserId, AprvCourseDto dto) {
		try {
			// 审核课程申请
			Map<String, String> param = new HashMap<String, String>();
			param.put("kcbh", dto.getKcbh());
			param.put("tableName", "LY_CKBZ_GXJW_KCSJSQL");
			param.put("sprbh", loginUserId);
			param.put("spyj", dto.getSpyj());
			param.put("spzt", dto.getSpzt());
			approvalDetailMapper.aprvCourse(param);
			String msg = param.get("result");// 返回最终审核状态

			if (PASS.equals(dto.getSpzt()) && SPZT_PASS.equals(msg)) { // 如果审批通过并且最终审批结果是审批通过
				Map<String, String> paramMap = new HashMap<String, String>();
				paramMap.put("kcbh", dto.getKcbh());
				approvalDetailMapper.syncCourse(paramMap);
			}
			if (NOPASS.equals(dto.getSpzt()) && SPZT_NOPASS.equals(msg)) { // 如果审批不通过
				ApplyCoursePo applyCoursePo = new ApplyCoursePo();
				applyCoursePo.setKch(dto.getKcbh());
				applyCoursePo.setXgr(loginUserId);
				applyCoursePo.setXgsj(new Date());
				applyCoursePo.setSpzt(SPZT_NOPASS);
				EntityWrapper<ApplyCoursePo> ew = new EntityWrapper<>();
				ew.where("kch={0}", applyCoursePo.getKch());
				applyCourseMapper.update(applyCoursePo, ew);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("审批失败", e);
		}
	}

	/**
	 * 批量审批课程申请
	 */
	@Override
	@Transactional
	public boolean batchAprvCourse(String loginUserId, List<AprvCourseDto> dtoList) {
		try {
			boolean isAprv = false;
			for (AprvCourseDto dto : dtoList) {
				isAprv = this.aprvCourse(loginUserId, dto);
			}
			return isAprv;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("批量审批失败", e);
		}
	}
}
